package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DesUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/member")
public class MemberController_Mobile {

    @Autowired
    private JedisPool jedisPool;

    //从服务端
    @Reference
    private MemberService memberService;


    @RequestMapping("/login")
    public Result login(@RequestBody Map map, HttpServletResponse response) throws Exception {
        //校验验证码是否正确
        //获取手机
        String telephone = (String) map.get("telephone");
        //获取验证码
        String validateCode = (String) map.get("validateCode");
        //从redis中取出登录验证码
        String redisValidateCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        //判断
        if (validateCode != null && redisValidateCode != null && validateCode.equals(redisValidateCode)) {
            //判断当前用户是否已经注册
            Member member = memberService.findByTelephone(telephone);
            if (member == null) { //不是会员
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberService.add(member);
            }
            //将登录信息添加到Cookies

            //对telephone进行加密
            String pwd = DesUtils.encryptDES(telephone, DesUtils.key, DesUtils.transformation, DesUtils.algorithm);
            /*String secretKey = new String();
            byte[] bytes = secretKey.getBytes();*/
            Cookie cookie = new Cookie("login_member_telephone", pwd);
            cookie.setPath("/");
            response.addCookie(cookie);
            //将会员信息保存带redis中
            //将实体类转换成json  因为redis中无法存实体类
            String member_json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone, 60 * 30, member_json);
            return new Result(true, MessageConstant.GET_PERSONINFO_FAIL);
        } else {//验证码校验失败
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    //校验登录
    @RequestMapping("/edit")
    public Result edit(HttpServletRequest request) throws Exception {

        Cookie[] cookies = request.getCookies();
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        if (cookies == null) {
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
        for (Cookie cookie : cookies) {
            cookieMap.put(cookie.getName(), cookie);
        }

        Cookie login_member_telephone = cookieMap.get("login_member_telephone");
        String secretKey = login_member_telephone.getValue();
        String telephone = DesUtils.decryptDES(secretKey, DesUtils.key, DesUtils.transformation, DesUtils.algorithm);

//        byte[] bytes = base64String2ByteFun(secretKey);
//        String telephone = AESUtils.aesDecrypt(bytes, secret());
        if (telephone != null && telephone.length() > 0) {
            return new Result(true, MessageConstant.GET_PERSONINFO_SUCCESS, telephone);
        } else {

            return new Result(false, MessageConstant.GET_PERSONINFO_FAIL);
        }

    }

    //编辑界面
    @RequestMapping("/show")
    public Result show(String telephone) {
        String json_member = jedisPool.getResource().get(telephone);
        System.out.println(json_member);
        if (json_member != null) {
            Member member = JSON.parseObject(json_member, Member.class);
            return new Result(true, MessageConstant.GET_PERSONINFO_SUCCESS, member);
        } else {
            return new Result(false, MessageConstant.GET_PERSONINFO_FAIL);
        }
    }

    //修改个人基本信息
    @RequestMapping("/editMemberInfo")
    public Result editMemberInfo(@RequestBody Member member){
        try {
             memberService.editMemberInfo(member);
            return new Result(true, MessageConstant.EDIT_MEMBERINDO_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBERINDO_FAIL);

        }

    }
}
